package services

import (
	"TaskAssignmentSystem/internal/admin/dao"
	"TaskAssignmentSystem/pkg/utils"
	"errors"
	"fmt"
	"time"
)

type SystemAdminService struct{}

func (s *SystemAdminService) GetList(page, pageSize int64) (res *dao.PageResponse[dao.SystemAdmin], err error) {
	var systemAdmin CommonService[dao.SystemAdmin]
	systemAdmin.Page = page
	systemAdmin.PageSize = pageSize
	return systemAdmin.SelectList()
}

type AdminServiceResponse struct {
	Token string `json:"token"`
}

func (s *SystemAdminService) VerifyLogin(userName, passWord string) (result *dao.SystemAdmin, err error) {
	var systemAdminDao dao.Common[dao.SystemAdmin]
	systemAdminDao.Query = map[string]interface{}{"user_name": userName}
	res, err := systemAdminDao.First(systemAdminDao.Query)
	if err != nil {
		return nil, errors.New("用户不存在")
	}
	if res.Status == 0 {
		return nil, errors.New("该管理员用户已经被封禁")
	}
	if utils.MD5(passWord) != res.PassWord {
		return nil, errors.New("用户密码错误")
	}
	return &res, nil
}

func (s *SystemAdminService) Login(userName, passWord, ip string) (data AdminServiceResponse, err error) {
	admin, err := s.VerifyLogin(userName, passWord)
	if err != nil {
		return data, err
	}
	claims := &CustomClaims{}
	claims.Id = admin.Id
	claims.UserName = admin.UserName
	claims.Type = admin.AdminType
	var systemAdminDao dao.Common[dao.SystemAdmin]
	systemAdminDao.UpdateOne(admin.Id, map[string]interface{}{"updated_at": time.Now(), "last_login_ip": ip})

	user_token_secret := fmt.Sprintf("%s_%d_%d", utils.ServerConf.JwtConf.AppSecret, admin.Id, time.Now().Unix())
	claims.AppSecret = []byte(user_token_secret)

	data.Token, err = claims.SetToken(24 * 60 * 60 * time.Second)
	return
}

func (s *SystemAdminService) Logout(id uint) error {
	claims := &CustomClaims{}
	claims.Id = id
	return claims.DelToken()
}

func (s *SystemAdminService) Edit(Id uint, set map[string]interface{}) error {
	var systemAdminDao dao.Common[dao.SystemAdmin]
	return systemAdminDao.UpdateOne(Id, set).Error
}

func (s *SystemAdminService) Created(systemAdmin dao.SystemAdmin) error {
	var systemAdminDao dao.Common[dao.SystemAdmin]
	return systemAdminDao.CreateOne(systemAdmin).Error
}

func (s *SystemAdminService) UpdatePassWord(id uint, oldPassWord string, set map[string]interface{}) error {
	var systemAdminDao dao.Common[dao.SystemAdmin]
	systemAdminDao.Query = map[string]interface{}{"id": id}
	if systemAdmin, err := systemAdminDao.First(systemAdminDao.Query); err != nil {
		return err
	} else {
		if systemAdmin.PassWord != utils.MD5(oldPassWord) {
			return errors.New("旧密码错误")
		}
	}
	return systemAdminDao.UpdateOne(id, set).Error
}

func (s *SystemAdminService) ChangeStatus(id uint, status int) error {
	var serve CommonService[dao.SystemAdmin]
	return serve.BatchUpdate([]uint{id}, map[string]interface{}{"status": status})
}

func (s *SystemAdminService) RemoveAdmin(id uint) error {
	var serve CommonService[dao.SystemAdmin]
	serve.Query = map[string]interface{}{"id": id}
	return serve.Remove()
}

func (u *SystemAdminService) VerifyAndQueryAdminUser(id uint) (bool, *dao.SystemAdmin, error) {
	var serve CommonService[dao.SystemAdmin]
	serve.Query = map[string]interface{}{"id": id}
	if count, err := serve.Count(); err != nil {
		return false, nil, err
	} else {
		if count == 0 {
			return false, nil, nil
		} else {
			if user, err := serve.First(); err != nil {
				return false, nil, err
			} else {
				return true, &user, nil
			}
		}
	}
}

func (s *SystemAdminService) SystemAdminTypeList(page, pageSize int64) (int64, []dao.SystemAdminType, error) {
	var serve CommonService[dao.SystemAdminType]
	serve.Page = page
	serve.PageSize = pageSize
	if tisList, err := serve.SelectList(); err != nil {
		return 0, nil, err
	} else {
		return tisList.Total, tisList.Data, nil
	}
}
